/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package introse;

import com.sun.net.ssl.internal.ssl.Debug;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 *
 * @author Alfred
 */
public class transactionManager {

    private List<transaction> transactionList;

    public transactionManager() {
        loadTransactions();
    }

    public Boolean hasInvoiceNumber(int invoiceNumber) {
        for (transaction trans : getTransactions()) {
            try {
                if (Integer.parseInt(trans.getInvoiceNumber().trim()) == invoiceNumber) {
                    return true;
                }
            } catch (Exception ex) {
            }
            System.out.print(trans.getInvoiceNumber());
        }
        return false;
    }

    public List<transaction> getTransactionsAtDate(Date selectedDate) {
        Calendar selectedDateNoTime = new GregorianCalendar();
        selectedDateNoTime.setTime(selectedDate);
        selectedDateNoTime.set(Calendar.HOUR_OF_DAY, 0);
        selectedDateNoTime.set(Calendar.MINUTE, 0);
        selectedDateNoTime.set(Calendar.SECOND, 0);
        selectedDateNoTime.set(Calendar.MILLISECOND, 0);

        List<transaction> tempTransactions = new ArrayList();

        for (transaction trans : transactionList) {
            Calendar transDate = new GregorianCalendar();
            transDate.setTime(trans.getDateAndTime());
            transDate.set(Calendar.HOUR_OF_DAY, 0);
            transDate.set(Calendar.MINUTE, 0);
            transDate.set(Calendar.SECOND, 0);
            transDate.set(Calendar.MILLISECOND, 0);

            if (transDate.equals(selectedDateNoTime)) {
                tempTransactions.add(trans);
            }
        }

        return tempTransactions;
    }

    public final Boolean loadTransactions() {
        try {
            databaseConn conn = new databaseConn();
            ResultSet transactions = conn.sqlRead("select *, SUM(`amount`) as `totalAmount` from"
                    + " (select `transaction`.`transactionNumber`,`invoiceNumber`, `transaction`.`dateAndTime`,`clientName`, `quantity`*`price` as `amount`, `remark`, `sourceName` from `transaction`"
                    + " left join `source` on `source`.`sourceNumber` = `transaction`.`sourceNumber`"
                    + " left join `client` on `client`.`clientNumber` = `transaction`.`clientNumber`"
                    + " left join `purchasedproduct` on `transaction`.`transactionNumber` = `purchasedproduct`.`transactionnumber`"
                    + " left join `productpricehistory` on `purchasedproduct`.`productNumber` = `productpricehistory`.`productNumber`"
                    + " where `productpricehistory`.`dateAndTime` <= `transaction`.`dateAndTime`"
                    + " group by `transaction`.`transactionNumber`, `purchasedproduct`.`productNumber`"
                    + " order by `transaction`.`dateAndTime` DESC) as `transactionWithMultipleDates`"
                    + " group by `transactionNumber`;");

            List<transaction> tempTransactionList = new ArrayList();
            while (transactions.next()) {
                tempTransactionList.add(new transaction(transactions.getInt("transactionNumber"), transactions.getString("invoiceNumber"), transactions.getTimestamp("dateAndTime"), transactions.getString("clientName"), transactions.getString("remark"), transactions.getString("sourceName"), transactions.getFloat("totalAmount")));
            }
            this.transactionList = tempTransactionList;
            return true;
        } catch (SQLException ex) {
            Debug.println("", ex.toString());
        }
        return false;
    }

    public List<transaction> getTransactions() {
        return transactionList;
    }

    public transaction getTransaction(int transNumber) {
        for (transaction trans : transactionList) {
            if (trans.getTransactionNumber() == transNumber) {
                return trans;
            }
        }
        return null;
    }

    public Boolean addTransaction(int clientNumber, int sourceNumber, String invoiceNumber, String remark) {
        databaseConn conn = new databaseConn();
        if (conn.sqlStatement(String.format("INSERT INTO `sashmirgasul`.`transaction` (`clientNumber`, `sourceNumber`, `invoiceNumber`, `dateAndTime`, `remark`) VALUES (%d, %d, '%s', NOW(), '%s');", clientNumber, sourceNumber, invoiceNumber, remark))) {
            loadTransactions();
            return true;
        }
        return false;
    }

    public Boolean addPurchasedProduct(int productNumber, int quantity) {
        databaseConn conn = new databaseConn();
        if (conn.sqlStatement(String.format("INSERT INTO `sashmirgasul`.`purchasedproduct` (`transactionNumber`, `productNumber`, `quantity`) VALUES ((select `transactionNumber` from `transaction` where `dateAndTime` = (select MAX(`dateAndTime`) from `transaction`)), %d, %d);", productNumber, quantity))) {
            return true;
        }
        return false;
    }
}
